package com.v2future.sjt.collegecheckingattendance;

import android.content.Intent;
import android.text.TextUtils;

import com.v2future.sjt.collegecheckingattendance.home.HomeActivity;
import com.v2future.sjt.collegecheckingattendance.home.bean.LoginResultBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.PreferencesUtil;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;

import org.xutils.http.RequestParams;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2016/1/29.
 */
public class WelcomeActivity extends MainActivity{

    private boolean isLoginOver = false;

    @Override
    public void init() {

    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_welcome);
    }

    @Override
    public void initAfter() {

        //登录操作
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String username = PreferencesUtil.getSharedStringData(WelcomeActivity.this, Constants.PREFERENCE_USERNAME);
                String pwd = PreferencesUtil.getSharedStringData(WelcomeActivity.this, Constants.PREFERENCE_PWD);
                String userType = PreferencesUtil.getSharedStringData(WelcomeActivity.this, Constants.PREFERENCE_USER_TYPE);
                if (TextUtils.isEmpty(username) ||
                        TextUtils.isEmpty(pwd) ||
                        TextUtils.isEmpty(userType)) {
                    isLoginOver = true;
                    startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                    finish();
                }
                else {
                    login(username, pwd, userType);
                }
            }
        }.start();

        //一定时间还是没有登录成功，则直接进入登录界面
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    sleep(10000);
                    if (isLoginOver) return;
                    NetUtils.cancelRequest();
                    startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                    finish();
                }catch (Exception e){}
            }
        }.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    public void login(final String userName, final String passWord, final String userType){
        RequestParams params = new RequestParams(formatUrl(Constants.LOGIN));
        params.addQueryStringParameter("passWord", passWord);
        params.addQueryStringParameter("userCode", userName);
        params.addQueryStringParameter("sourceTypeId", userType);
        NetUtils.doGetRequest(this, params, new NetTaskListener<LoginResultBean>() {

            @Override
            public void onSuccess(LoginResultBean result) {
                //保存当前用户到本地内存
                isLoginOver = true;
                getBaseApplication().insertCathe(Constants.CACHE_KEY_USER_INFO, result);
                startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
                isLoginOver = true;
                finish();
            }

            @Override
            public void onError() {
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                isLoginOver = true;
                finish();
            }

            @Override
            public void onInterError(LoginResultBean result) {
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                isLoginOver = true;
                finish();
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                isLoginOver = true;
                finish();
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {
                isLoginOver = true;
            }

            @Override
            public void onSuccessCache(LoginResultBean result) {
                //未使用缓存
            }
        });
    }
}
